
package bibliotheque.server;

import bibliotheque.transaction.Transaction;
import bibliotheque.transaction.TransactionId;
import bibliotheque.wave.WaveAction;
import bibliotheque.wave.WaveAgent;
import java.util.ArrayList;

public class ResolvAction extends WaveAction{
    
    private TransactionId id;
    private ArrayList<String> filtre;
    private Server.SelectionType type;
    private boolean resolv;
    
    public ResolvAction(TransactionId id, ArrayList<String> filtre, Server.SelectionType type) {
        this.id = id;
        this.filtre = filtre;
        this.type = type;
        this.resolv = true;
    }
    
    public TransactionId GetId() {
        return(this.id);
    }
    
    public ArrayList<String> GetFiltre() {
        return(this.filtre);
    }
    
    public Server.SelectionType GetType() {
        return(this.type);
    }
    
    public void PropagateBefore(WaveAgent agent) throws Exception {
        if (agent instanceof Server) {
            Server server = (Server)agent;
            Transaction t;
            try {
                server.mutex.acquireUninterruptibly();
                if(server.state == Server.State.STOP) throw new Exception("Selection failed cause server state is stopped.");
               
                server.transaction_manager.ResolvInit(this.id, this.filtre, this.type);
            } catch (Exception ex) {
                server.ErrorOccured(ex.getMessage());
                throw ex;
            } finally {
                server.mutex.release();
            }
        }
    }
    
    public void Acknowledge(WaveAgent agent) throws Exception {
        if (agent instanceof Server) {
            Server server = (Server)agent;
            try {
                server.mutex.acquireUninterruptibly();
                if(server.state == Server.State.STOP) throw new Exception("Selection failed cause server state is stopped.");
                
                this.resolv = server.transaction_manager.ResolvUpdate(this.id, this.resolv);
            } catch (Exception ex) {
                server.ErrorOccured(ex.getMessage());
                throw ex;
            } finally {
                server.mutex.release();
            }
        }
    }
    
     public void LocalEnd(WaveAgent agent) throws Exception {
        if (agent instanceof Server) {
            Server server = (Server)agent;
            try {
                server.mutex.acquireUninterruptibly();
                if(server.state == Server.State.STOP) throw new Exception("Selection failed cause server state is stopped.");
                
                this.resolv = server.transaction_manager.ResolvResult(this.id);
               if(server.state == Server.State.STOPPING && server.transaction_manager.GetNbUseDocument() == 0 && server.GetNbWaves() == 0) {
                    server.state = Server.State.STOP;
                    server.StateChanged(server.state.toString());
                }
            } catch (Exception ex) {
                server.ErrorOccured(ex.getMessage());
                throw ex;
            } finally {
                server.mutex.release();
            }
        }
    }
    
    public void GlobalEnd(WaveAgent agent) throws Exception {
        if (agent instanceof Server) {
            Server server = (Server)agent;
            try {
                server.mutex.acquireUninterruptibly();
                if(server.state == Server.State.STOP) throw new Exception("Selection failed cause server state is stopped.");
                
                if(this.resolv) {
                    server.Initiate(new PreSelectionAction(this.id));
                } else {
                    server.Initiate(this);
                }
            } catch (Exception ex) {
                server.ErrorOccured(ex.getMessage());
                throw ex;
            } finally {
                server.mutex.release();
            }
        }
    }
}
